{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 示例脚本"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "本脚本目的在于验证工作环境是否安装配置成功，在完成PPT展示的安装教程之后，需要在命令行窗口运行如下命令，使得本脚本工作。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "pip install pygame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 黑客帝国代码雨"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pygame 1.9.4\n",
      "Hello from the pygame community. https://www.pygame.org/contribute.html\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-6379d4a9c84f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     35\u001b[0m         \u001b[0mx\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     36\u001b[0m         \u001b[1;32mfor\u001b[0m \u001b[0mc\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mtext\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 37\u001b[1;33m             \u001b[0mscreen\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mblit\u001b[0m\u001b[1;33m(\u001b[0m \u001b[0mfont\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrender\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m255\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     38\u001b[0m             \u001b[0mx\u001b[0m\u001b[1;33m+=\u001b[0m\u001b[1;36m20\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     39\u001b[0m         \u001b[0my\u001b[0m\u001b[1;33m-=\u001b[0m\u001b[0mfont_height\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "import pygame\n",
    "import sys\n",
    "from pygame.locals import *\n",
    "from sys import exit\n",
    "import random\n",
    " \n",
    "pygame.init()\n",
    "SCREEN_SIZE = (960, 640)\n",
    "screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)\n",
    " \n",
    "font = pygame.font.SysFont(\"arial\", 16);\n",
    "font_height = font.get_linesize()\n",
    "#event_text = []\n",
    "texts=[['0']*80]\n",
    " \n",
    "while True:\n",
    "    event= pygame.event.poll()\n",
    "    i=0\n",
    "    t=80\n",
    "    tx=[]\n",
    "    while i<t:\n",
    "        tx.append(chr(random.randint(33, 126)))\n",
    "        i+=1\n",
    "    texts.append(tx)\n",
    "    texts=texts[-SCREEN_SIZE[1]//font_height:]\n",
    "    #这个切片操作保证了event_text里面只保留一个屏幕的文字\n",
    " \n",
    "    if event.type == QUIT:\n",
    "        sys.exit()\n",
    " \n",
    "    screen.fill((0, 0, 0))#屏幕填充黑色\n",
    "    y = SCREEN_SIZE[1]-font_height\n",
    "    #找一个合适的起笔位置，最下面开始但是要留一行的空\n",
    "    for text in texts:\n",
    "        x=0\n",
    "        for c in text:\n",
    "            screen.blit( font.render(c, True, (0, 255, 0)), (x, y) )\n",
    "            x+=20       \n",
    "        y-=font_height\n",
    "        #把笔提一行\n",
    "        i=i+1\n",
    " \n",
    "    pygame.display.update()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 黑客帝国代码雨2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pygame 1.9.4\n",
      "Hello from the pygame community. https://www.pygame.org/contribute.html\n"
     ]
    },
    {
     "ename": "error",
     "evalue": "display Surface quit",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31merror\u001b[0m                                     Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-8af801fecd57>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     64\u001b[0m             \u001b[0mexit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     65\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 66\u001b[1;33m     \u001b[0mscreen\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfill\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     67\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mgroup_count\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     68\u001b[0m         \u001b[0mgroup\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mWord\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mFONT_SIZE\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m-\u001b[0m\u001b[0mFONT_SIZE\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31merror\u001b[0m: display Surface quit"
     ]
    }
   ],
   "source": [
    "# imports\n",
    "import pygame\n",
    "import random\n",
    "from pygame.locals import *\n",
    "from random import randint\n",
    "\n",
    "\n",
    "# define\n",
    "SCREEN_WIDTH  = 900\n",
    "SCREEN_HEIGHT = 600\n",
    "LOW_SPEED  = 10\n",
    "HIGH_SPEED = 20\n",
    "LOW_SIZE  = 5\n",
    "HIGH_SIZE = 50\n",
    "FONT_SIZE = 10\n",
    "FONT_NAME = \"myfont.ttf\"\n",
    "FREQUENCE = 10\n",
    "times = 0\n",
    "\n",
    "# def func\n",
    "def randomcolor() :\n",
    "    return (randint(0,255),randint(0,255),randint(0,255))\n",
    "def randomspeed() :\n",
    "    return randint(LOW_SPEED,HIGH_SPEED)\n",
    "def randomposition() :\n",
    "    return (randint(0,SCREEN_WIDTH),randint(0,SCREEN_HEIGHT))\n",
    "def randomsize() :\n",
    "    return randint(LOW_SIZE,HIGH_SIZE)\n",
    "def randomoname() :\n",
    "    return randint(0,100000)\n",
    "def randomvalue() :\n",
    "    return randint(0,100)                               #this is your own display number range\n",
    "\n",
    "# class of sprite\n",
    "class Word(pygame.sprite.Sprite) :\n",
    "    def __init__(self,bornposition) :\n",
    "        pygame.sprite.Sprite.__init__(self)\n",
    "        self.value = chr(random.randint(33, 126))\n",
    "        self.font = pygame.font.Font(FONT_NAME,FONT_SIZE)\n",
    "        self.image = self.font.render(str(self.value),True,randomcolor())\n",
    "        self.speed = randomspeed()\n",
    "        self.rect = self.image.get_rect()\n",
    "        self.rect.topleft = bornposition\n",
    "\n",
    "    def update(self) :\n",
    "        self.rect = self.rect.move(0,self.speed)\n",
    "        if self.rect.top > SCREEN_HEIGHT :\n",
    "            self.kill()\n",
    "\n",
    "# init the available modules\n",
    "pygame.init()\n",
    "screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))\n",
    "pygame.display.set_caption(\"ViatorSun HACKER EMPIRE CodeRain\")\n",
    "clock = pygame.time.Clock()\n",
    "group = pygame.sprite.Group()\n",
    "group_count = int(SCREEN_WIDTH / FONT_SIZE)\n",
    "\n",
    "# mainloop\n",
    "while True :\n",
    "    time = clock.tick(FREQUENCE)\n",
    "    for event in pygame.event.get() :\n",
    "        if event.type == QUIT :\n",
    "            pygame.quit()\n",
    "            exit()\n",
    "\n",
    "    screen.fill((0,0,0))\n",
    "    for i in range(0,group_count) :\n",
    "        group.add(Word((i * FONT_SIZE,-FONT_SIZE)))\n",
    "\n",
    "    group.update()\n",
    "    group.draw(screen)\n",
    "    pygame.display.update()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font color=gray> 本脚本程序来源于CSDN，教学使用，非盈利目的 </font>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
